package modules.majiang.viewManager
{
	/**
	 *
	 * @author xumin.xu 2013-1-24
	 */

	import com.netease.protobuf.UInt64;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.entity.AutoActionType;
	
	import protobuf.MSPKG_INIT_TILES_NTF;

	public class ActionNextViewManager extends ViewManager
	{
		private var tableId:UInt64;

		private var autoCall:MovieClip;

		private var callAny:MovieClip;

		private var autoFold:MovieClip;

		private var autoFoldOrCheck:MovieClip;

		private var autoCheck:MovieClip;

		private var autoCheckButton:MovieClip;

		private var autoCallAnyButton:MovieClip;

		private var autoCheckFoldButton:MovieClip;

		private var autoFoldButton:MovieClip;

		/** 当前自动操作类型 */
		public var _autoActionType:String = AutoActionType.NULL;

		public function ActionNextViewManager(tableId:UInt64, mainView:MovieClip)
		{
			super();

			this.tableId = tableId;

			this.mainUI = mainView;

			autoCall = mainView.autoCallAny_mc;
			callAny = mainView.autoCallAny_mc;
			autoFold = mainView.autoFold_mc;
			autoFoldOrCheck = mainView.autoCheckFold_mc;
			autoCheck = mainView.autoCheck_mc;

			autoCall.mouseEnabled = false;
			autoCall.mouseChildren = false;
			callAny.mouseEnabled = false;
			callAny.mouseChildren = false;
			autoFold.mouseEnabled = false;
			autoFold.mouseChildren = false;
			autoFoldOrCheck.mouseEnabled = false;
			autoFoldOrCheck.mouseChildren = false;
			autoCheck.mouseEnabled = false;
			autoCheck.mouseChildren = false;

			autoCheckButton = mainView.check_btn;
			autoCallAnyButton = mainView.callAny_btn;
			autoCheckFoldButton = mainView.checkFold_btn;
			autoFoldButton = mainView.flod_btn;

			autoActionType = AutoActionType.NULL;

			mainView.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			mainView.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);
		}

		private function onAddToStage(event:Event):void
		{
			//第一轮的7张牌
			dispatcher.addEventListener(MSProto.MSID_INIT_TILES_NTF.toString(), function(event:GameEvent):void
			{
				var pkg:MSPKG_INIT_TILES_NTF = event.data;
				if (pkg.tableId.toString() != tableId.toString())
					return;
				autoActionType = AutoActionType.NULL;
			});

			mainUI.addEventListener(MouseEvent.CLICK, onClick);
		}

		private function removeFromStage(event:Event):void
		{

		}

		public function set autoActionType(type:String):void
		{
			_autoActionType = type;

			autoCall.visible = false;
			callAny.visible = false;
			autoFold.visible = false;
			autoFoldOrCheck.visible = false;
			autoCheck.visible = false;

			switch (type)
			{
				case AutoActionType.AUTOCALL:
					autoCall.visible = true;
					break;
				case AutoActionType.AUTOCHECK:
					autoCheck.visible = true;
					break;
				case AutoActionType.AUTOFOLD:
					autoFold.visible = true;
					break;
				case AutoActionType.AUTOFOLDORCHECK:
					autoFoldOrCheck.visible = true;
					break;
				case AutoActionType.CALLANY:
					callAny.visible = true;
					break;
			}
		}

		public function get autoActionType():String
		{
			return _autoActionType;
		}

		/**
		 * 修改自动操作选项
		 **/
		private function onClickAutoActionHandler(type:String):void
		{
			autoActionType = autoActionType == type ? AutoActionType.NULL : type;
		}

		private function onClick(event:MouseEvent):void
		{
			switch (event.target)
			{
				case autoCheckButton:
					onClickAutoActionHandler(AutoActionType.AUTOCHECK);
					break;
				case autoCallAnyButton:
					onClickAutoActionHandler(AutoActionType.CALLANY);
					break;
				case autoCheckFoldButton:
					onClickAutoActionHandler(AutoActionType.AUTOFOLDORCHECK);
					break;
				case autoFoldButton:
					onClickAutoActionHandler(AutoActionType.AUTOFOLD);
					break;
			}
		}
	}
}
